// Evol functions.
// Author:
//     gumi
// Description:
//     generic quest debug functions



// GenericQuestDebug
//     makes a generic quest debug menu for the given quest when the server
//     is in debug mode.

function	script	GenericQuestDebug	{
    if (!debug)
    {
        select
            menuimage("actions/back", l("Go back"));

        @menuret = -0x7FFFFFFF;
        return 1;
    }

    mes l("Choose desired quest state:");

    deletearray .@vals;
    .@menustr$ = menuimage("actions/abort", l("Do nothing")) + ":";
    .@vals[0] = -0x7FFFFFFF;
    .@cnt = 1;

    for (.@f = 1; .@f < getargcount(); .@f += 2)
    {
        if (getarg(.@f) != "")
        {
            .@qv = getarg(.@f + 1);
            .@s$ = menuimage("actions/" + (.@qv ? "edit" : "reset"), "[" + .@qv + "] " + getarg(.@f));
            .@menustr$ += (.@qv < 0 ? getarg(.@f) : .@s$) + ":";
            .@vals[.@cnt] = .@qv;
            ++.@cnt;
        }
    }

    if (getgroupid() >= 99)
    {
        .@menustr$ += menuimage("actions/nuke", l("Set state manually"));
        .@vals[.@cnt] = -0x7FFFFFFE;
    }


    @menu = 255;
    @menuret = -1;
    select(.@menustr$);
    if (@menu == 255)
        return -1;

    --@menu;
    if (@menu < 0 || @menu >= getarraysize(.@vals))
        return -1;

    @menuret = .@vals[@menu];

    switch (@menuret)
    {
        case -0x7FFFFFFE: input @menuret; setq getarg(0,0), @menuret;
        case -0x7FFFFFFF: return 1;
    }

    if (@menuret >= 0)
    {
        setq getarg(0,0), @menuret;
    }
    return;
}



// selectd
//     like a normal select() but gives access to quest debug
//
//     quest id can either be passed as first argument
//     or as the .quest_debug npc variable

function	script	selectd	{
    .@menustr$ = rif(debug, menuaction(l("debug")));
    .@count = getargcount();
    .@f = 0;
    .@questID = getvariableofnpc(.quest_debug, strnpcinfo(0));

    if (.@count > 1 && !isstr(getarg(0)))
    {
        ++.@f;
        .@questID = getarg(0);
    }

    for (; .@f < .@count; ++.@f)
    {
        .@menustr$ += ":" + getarg(.@f);
    }

    @menu = 255;
    select .@menustr$;

    switch (@menu)
    {
        case -1:
        case 255: return -1;
        default: @menu -= 1; return @menu; // FIXME: here I would have done `return --@menu;` but hercules prevents this
        case 1: callfunc "QuestDebug" + .@questID; closedialog; end;
    }
}
